/*
 * @Desc: 构造树型结构数据
 * @Author: feim
 * @return: {Object}
 * @Date: 2022-06-23 17:45:42
*/

import { set } from "nprogress";


const baseUrl = process.env.VUE_APP_ROUTER_BASE;
/*
* @des 拍平权限树
*  */
export function getMenuAllItem(tree) {
  let res = [{
    uri: '/app/ucenter'
  }]
  for (const item of tree) {
    const { childNodes, ...i } = item
    if (childNodes && childNodes.length) {
      res = res.concat(getMenuAllItem(childNodes))
    }
    i.pid = i.parentResourceId
    i.id = i.resourceId
    i.type = i.resourceType;
    if(i.type === 1) res.push(i)

  }
  return res
}

/*
 * @Desc: 给菜单添加uri
 * @params: {String}
 * @return: {Object}
*/
export function setMenuUri(allTree) {
  return setMenu(allTree)

}

export function setMenuParentUri(group, baseUri){
  for(let item of group) {
    const { childNodes, ...cur } = item
    if(!cur.uri) {
      cur.uri = getCurrentUriFromChild(nodeChild, baseUri)
    }
  }
}

function getCurrentUriFromChild(node, baseUri){
  for(let item of node) {
    if(item.uri){
      uri.replace(baseUri + '/', '')
      return baseUri + '/' + uri.split('/')[0]
    }
  }
}

// ----------------------- 菜单重置 ------------------------
function setMenu(data){
  const menus = [];
  data.forEach(item => {
    // 用户中心菜单<a-icon type="alert" />
    if (item.nameItemLabel === 'ucenter') {
        menus.push(...filter([Object.assign(item, { resourceType: 1, uri: '' })]))
    } else {
        if (Array.isArray(item.resourceNodes)) {
            menus.push(...filter(item.resourceNodes.map(item => Object.assign(item))))
        }
    }
  })
  return menus
  console.log(menus)
}

function filter(arr) {
  const menuList = arr.filter(item => item.resourceType === 1);
  return menuList.map(item => {
      let children = []
      if (Array.isArray(item.resourceNodes)) {
          children = filter(item.resourceNodes)
      } else if (Array.isArray(item.childNodes)) {
          children = filter(item.childNodes)
      }
      const extra = {
          title: item.name || '',
          href: item.uri || ''
      }
      if (Array.isArray(children) && children.length) {
          extra.children = children
      }
      return Object.assign(item, extra)
  })
}



function getChecked(menus, result, parents) {
  menus.forEach(item => {
      if (item.href === location.pathname) {
          result.selectedKeys.push(item.resourceId)
          result.openKeys = parents.map(item => item.resourceId || item.appId)
      } else if (Array.isArray(item.children)) {
          getChecked(item.children, result, [...parents, item])
      }
  })
}

export function getMenu(data) {
  // return
  const menus = [];
  data.forEach(item => {
      // 用户中心菜单<a-icon type="alert" />
      if (item.nameItemLabel === 'ucenter') {
          // console.log(item)
          Object.assign(item, { resourceType: 1 })
          // console.log(item)
          // console.log(filterNode([item]))
          menus.push(filterNode([item]))
      } else {
          if (Array.isArray(item.resourceNodes)) {
              // menus.push(...filter(item.resourceNodes.map(item => Object.assign(item, getIcon('resource', item.resourceId)))))
          }
      }
  })
  // resourceType: 1菜单，2按钮，3接口
  return menus
}
